package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.dto.BehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.IApUserFanService;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * <p>
 * APP用户粉丝信息表 服务实现类
 * </p>
 *
 * @author JinCc
 * @since 2024-08-21
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ApUserFanServiceImpl extends ServiceImpl<ApUserFanMapper, ApUserFan> implements IApUserFanService {

    private final ApUserFollowMapper userFollowMapper;
    private final ApUserFanMapper userFanMapper;

    /**
     * 粉丝和关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(BehaviorDto dto) {
        //不能自己关注自己
        WmUser user = WmThreadLocalUtil.getUser();
        if (ObjectUtils.isEmpty(user.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.HALF_SUCCESS);
        }
        if (dto.getAuthorId().equals(user.getApAuthorId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.HALF_SUCCESS);
        }
        //关注
        LambdaQueryWrapper<ApUserFollow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(user.getId()), ApUserFollow::getUserId, user.getId());
        wrapper.eq(!ObjectUtils.isEmpty(dto.getAuthorId()), ApUserFollow::getFollowId, dto.getAuthorId());
        ApUserFollow follow = userFollowMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(follow)){
            ApUserFollow userFollow = new ApUserFollow();
            userFollow.setUserId(user.getId());
            userFollow.setFollowId(Math.toIntExact(dto.getAuthorId()));
            userFollow.setFollowName(user.getNickname());
            userFollow.setLevel(Short.valueOf(dto.getOperation()));
            userFollow.setOperation(Short.valueOf(dto.getOperation()));
            userFollow.setIsNotice((short) 0);
            userFollow.setCreatedTime(LocalDateTime.now());
            userFollowMapper.insert(userFollow);
        }else {
            follow.setOperation(Short.valueOf(dto.getOperation()));
            userFollowMapper.updateById(follow);
        }



        //粉丝
        LambdaQueryWrapper<ApUserFan> ApWrapper = new LambdaQueryWrapper<>();
        ApWrapper.eq(!ObjectUtils.isEmpty(dto.getAuthorId()), ApUserFan::getUserId, dto.getAuthorId());
        ApWrapper.eq(!ObjectUtils.isEmpty(user.getId()), ApUserFan::getFansId, user.getId());
        ApUserFan userFan = userFanMapper.selectOne(ApWrapper);
        if (ObjectUtils.isEmpty(userFan)){
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(Math.toIntExact(dto.getAuthorId()));
            apUserFan.setFansId(user.getId());
            apUserFan.setFansName(user.getNickname());
            apUserFan.setLevel((short) 0);
            apUserFan.setCreatedTime(LocalDateTime.now());
            apUserFan.setIsDisplay((short) 0);
            apUserFan.setIsShieldLetter((short) 0);
            apUserFan.setIsShieldComment((short) 0);
            userFanMapper.insert(apUserFan);
        }else {
            userFanMapper.deleteById(userFan);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
